using Microsoft.Win32;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text.RegularExpressions;
using System.Utility.System;
using System.Web;

namespace System.Utility.Helper
{
	public class Sys
	{
		public Sys()
		{
		}

		public static List<Process> FindProcessByProtId(int id)
		{
			List<Process> processes;
			if (id > 0)
			{
				Process process = new Process();
				List<Process> processes1 = new List<Process>();
				try
				{
					try
					{
						process.StartInfo.FileName = "cmd.exe";
						process.StartInfo.CreateNoWindow = true;
						process.StartInfo.UseShellExecute = false;
						process.StartInfo.RedirectStandardOutput = true;
						process.StartInfo.RedirectStandardInput = true;
						process.StartInfo.RedirectStandardError = true;
						process.Start();
						process.StandardInput.WriteLine(string.Format("netstat -aon|findstr {0}", id));
						process.StandardInput.WriteLine("exit");
						StreamReader standardOutput = process.StandardOutput;
						while (!standardOutput.EndOfStream)
						{
							string str = standardOutput.ReadLine();
							if (TypeExtension.IsValid(str))
							{
								Process process1 = ReadLine(str);
								if (process1 != null)
								{
									processes1.Add(process1);
								}
							}
						}
						processes = processes1;
					}
					catch (Exception exception)
					{
						processes = null;
					}
				}
				finally
				{
					process.Close();
				}
			}
			else
			{
				processes = null;
			}
			return processes;
		}

		public static string GetClientHostIP()
		{
			string userHostAddress;
			if (HttpContext.Current == null)
			{
				string hostName = Dns.GetHostName();
				IPAddress[] addressList = Dns.GetHostEntry(hostName).AddressList;
				if ((int)addressList.Length > 0)
				{
					string[] array = Enumerable.ToArray((
					    from s in Enumerable.ToList(addressList)
					    where !s.IsIPv6LinkLocal
					    select s.ToString()));
					userHostAddress = string.Format("{0}：{1}", hostName, string.Join(";", array));
					return userHostAddress;
				}
			}
			userHostAddress = HttpContext.Current.Request.UserHostAddress;
			return userHostAddress;
		}

		public static string GetCurrentDesktop()
		{
			string str;
			RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
			str = (registryKey == null ? string.Empty : TypeExtension.ToSafeString(registryKey.GetValue("Desktop")));
			return str;
		}

		public static string GetHostName()
		{
			return Dns.GetHostName();
		}

		[DllImport("wininet", CharSet=CharSet.None, ExactSpelling=false)]
		private static extern bool InternetGetConnectedState(out int connectionDescription, int reservedValue);

		public static bool IsConnectedBiadu()
		{
			PingReply pingReply = (new Ping()).Send("www.baidu.com");
			return ((pingReply == null ? true : pingReply.Status != IPStatus.Success) ? false : true);
		}

		public static bool IsConnectedInternet()
		{
			int num = 0;
			return InternetGetConnectedState(out num, 0);
		}

		public static bool IsRunAsAdmin()
		{
			return (new WindowsPrincipal(WindowsIdentity.GetCurrent())).IsInRole(WindowsBuiltInRole.Administrator);
		}

		public static void KillProcess(string proName)
		{
			if (!TypeExtension.IsInvalid(proName))
			{
				Process[] processesByName = Process.GetProcessesByName(proName);
				if (!TypeExtension.IsInvalid(processesByName))
				{
					Process[] processArray = processesByName;
					for (int i = 0; i < (int)processArray.Length; i++)
					{
						Process process = processArray[i];
						try
						{
							process.Kill();
							process.Close();
							process.Dispose();
						}
						catch
						{
						}
					}
				}
			}
		}

		public static void KillProcess(int id, string[] exceptPros)
		{
			List<Process> processes = FindProcessByProtId(id);
			if (!TypeExtension.IsInvalid(processes))
			{
				processes.ForEach((Process p) => {
					if (!Enumerable.Contains(exceptPros, p.ProcessName))
					{
						p.Kill();
						p.Close();
						p.Dispose();
					}
				});
			}
		}

		private static Process ReadLine(string line)
		{
			Process processById;
			string str = Regex.Replace(line, "\\s+", ",");
			char[] chrArray = new char[] { ',' };
			line = str.Trim(chrArray);
			chrArray = new char[] { ',' };
			string[] strArrays = line.Split(chrArray, StringSplitOptions.RemoveEmptyEntries);
			if (((int)strArrays.Length == 0 || strArrays[0] != "TCP" ? false : !(strArrays[3] != "LISTENING")))
			{
				int safeInt = TypeExtension.ToSafeInt(strArrays[4]);
				processById = Process.GetProcessById(safeInt);
			}
			else
			{
				processById = null;
			}
			return processById;
		}

		public static void SetPathEnvironment(string value)
		{
			try
			{
				string environmentVariable = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine);
				if (!environmentVariable.Contains(value))
				{
					environmentVariable = (!environmentVariable.EndsWith(";") ? string.Concat(environmentVariable, ";", value) : string.Concat(environmentVariable, value));
					Environment.SetEnvironmentVariable("PATH", environmentVariable, EnvironmentVariableTarget.Machine);
				}
				else
				{
					return;
				}
			}
			catch
			{
			}
		}
	}
}